package com.zb.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.entity.ZjInstructionBook;
import com.zb.entity.ZjKnowledgeType;
import com.zb.entity.ZjKnowledgeTypeInstructionBook;
import com.zb.extend.KnowledgeTypeInstructionBookExtend;
import com.zb.extend.KnowledgeTypeVideoExtend;
import com.zb.mapper.ZjKnowledgeTypeInstructionBookMapper;
import com.zb.service.ZjInstructionBookService;
import com.zb.service.ZjKnowledgeTypeInstructionBookService;
import com.zb.entity.KnowledgeTypeRelationVo;
import com.zb.service.ZjKnowledgeTypeService;
import com.zb.vo.KnowledgeInfoVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 知识库分类说明书表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-11-13
 */
@Service
public class ZjKnowledgeTypeInstructionBookServiceImpl extends ServiceImpl<ZjKnowledgeTypeInstructionBookMapper, ZjKnowledgeTypeInstructionBook> implements ZjKnowledgeTypeInstructionBookService {
    @Resource
    private ZjInstructionBookService instructionBookService;
    @Resource
    private ZjKnowledgeTypeService knowledgeTypeService;

    // 新增、删除
    @Override
    public void save(Long custAccId, Long instructionBookId, List<KnowledgeTypeRelationVo> knowledgeTypeVos, List<Long> delIds) {
        if (CollectionUtil.isNotEmpty(delIds)) {
            this.removeByIds(delIds);
        }
        if (CollectionUtil.isEmpty(knowledgeTypeVos)) {
            return;
        }
        List<ZjKnowledgeTypeInstructionBook> adds = new ArrayList<>();
        List<ZjKnowledgeTypeInstructionBook> updates = new ArrayList<>();
        ZjKnowledgeTypeInstructionBook typeInstructionBook = null;
        for (KnowledgeTypeRelationVo vo : knowledgeTypeVos) {
            typeInstructionBook = new ZjKnowledgeTypeInstructionBook();
            BeanUtil.copyProperties(vo, typeInstructionBook);
            if (null != typeInstructionBook.getId()) {
                updates.add(typeInstructionBook);
                continue;
            }
            typeInstructionBook.setId(IdWorker.getId());
            typeInstructionBook.setCustAccId(custAccId);
            typeInstructionBook.setInstructionBookId(instructionBookId);
            adds.add(typeInstructionBook);
        }
        if (CollectionUtil.isNotEmpty(adds)) this.saveBatch(adds);
        if (CollectionUtil.isNotEmpty(updates)) this.updateBatchById(updates);
    }

    // 根据说明书ID获取知识库分类ID
    @Override
    public List<ZjKnowledgeTypeInstructionBook> getByInstructionBookId(Long instructionBookId) {
        QueryWrapper<ZjKnowledgeTypeInstructionBook> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id");
        wrapper.eq("instruction_book_id", instructionBookId);
        return this.list(wrapper);
    }

    // 根据说明书ID删除
    @Override
    public void deleteBatchByInstructionBookId(List<Long> instructionBookIds) {
        if (CollectionUtil.isEmpty(instructionBookIds)) {
            return;
        }
        QueryWrapper<ZjKnowledgeTypeInstructionBook> wrapper = new QueryWrapper<>();
        wrapper.in("instruction_book_id", instructionBookIds);
        this.remove(wrapper);
    }

    // 根据知识库分类ID获取关联说明书信息
    @Override
    public List<KnowledgeTypeInstructionBookExtend> getByKnowledgeTypeIds(List<Long> knowledgeTypeIds) {
        if (CollectionUtil.isEmpty(knowledgeTypeIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeTypeInstructionBook> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "instruction_book_id");
        wrapper.in("knowledge_type_id", knowledgeTypeIds);
        List<ZjKnowledgeTypeInstructionBook> typeInstructionBooks = this.list(wrapper);
        if (CollectionUtil.isEmpty(typeInstructionBooks)) {
            return new ArrayList<>();
        }

        List<Long> bookIds = typeInstructionBooks.stream().map(ZjKnowledgeTypeInstructionBook::getInstructionBookId).distinct().toList();
        QueryWrapper<ZjInstructionBook> bookQueryWrapper = new QueryWrapper<>();
        bookQueryWrapper.select("id", "name", "image");
        bookQueryWrapper.in("id", bookIds);
        List<ZjInstructionBook> instructionBooks = instructionBookService.list(bookQueryWrapper);

        return typeInstructionBooks.stream().map(type -> {
            KnowledgeTypeInstructionBookExtend extend = new KnowledgeTypeInstructionBookExtend();
            BeanUtil.copyProperties(type, extend);
            ZjInstructionBook book = instructionBooks.stream().filter(f -> Objects.equals(f.getId(), type.getInstructionBookId())).findFirst().orElse(null);
            if (null != book) {
                extend.setName(book.getName());
                extend.setImage(book.getImage());
            }
            return extend;
        }).toList();
    }

    // 根据知识仓库分类ID获取说明书
    @Override
    public List<KnowledgeInfoVo> getInstructionBook(Long knowledgeTypeId) {
        List<Long> knowledgeTypeIds = new ArrayList<>();
        knowledgeTypeIds.add(knowledgeTypeId);
        List<KnowledgeTypeInstructionBookExtend> bookExtendList = getByKnowledgeTypeIds(knowledgeTypeIds);

        QueryWrapper<ZjKnowledgeType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.select("name");
        typeQueryWrapper.eq("id", knowledgeTypeId);
        ZjKnowledgeType knowledgeType = knowledgeTypeService.getOne(typeQueryWrapper);

        return bookExtendList.stream().map(e -> {
            return new KnowledgeInfoVo(e.getInstructionBookId(), e.getName(), e.getImage(), knowledgeType.getName());
        }).toList();
    }

    // 根据说明书ID获取关联说明书信息
    @Override
    public List<KnowledgeTypeInstructionBookExtend> getByInstructionBookIds(List<Long> instructionBookIds) {
        if (CollectionUtil.isEmpty(instructionBookIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeTypeInstructionBook> wrapper = new QueryWrapper<>();
        wrapper.select("id", "knowledge_type_id", "instruction_book_id");
        wrapper.in("instruction_book_id", instructionBookIds);
        List<ZjKnowledgeTypeInstructionBook> typeInstructionBooks = this.list(wrapper);
        if (CollectionUtil.isEmpty(typeInstructionBooks)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjKnowledgeType> typeQueryWrapper = new QueryWrapper<>();
        typeQueryWrapper.select("id", "name");
        typeQueryWrapper.in("id", typeInstructionBooks.stream().map(ZjKnowledgeTypeInstructionBook::getKnowledgeTypeId).distinct().toList());
        List<ZjKnowledgeType> knowledgeTypeList = knowledgeTypeService.list(typeQueryWrapper);

        return typeInstructionBooks.stream().map(type -> {
            KnowledgeTypeInstructionBookExtend extend = new KnowledgeTypeInstructionBookExtend();
            BeanUtil.copyProperties(type, extend);
            extend.setName(knowledgeTypeList.stream().filter(f -> Objects.equals(f.getId(), type.getKnowledgeTypeId())).map(ZjKnowledgeType::getName).findFirst().orElse(""));
            return extend;
        }).toList();
    }
}
